/**
 * @description
 */


package com.yjx.crm.controller;

import com.yjx.crm.annotaion.RequiredPermission;
import com.yjx.crm.base.BaseController;
import com.yjx.crm.base.ResultInfo;
import com.yjx.crm.enums.StateStatus;
import com.yjx.crm.query.SaleChanceQuery;
import com.yjx.crm.service.SaleChanceService;
import com.yjx.crm.utils.CookieUtil;
import com.yjx.crm.utils.LoginUserUtil;
import com.yjx.crm.vo.SaleChance;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@Controller
@RequestMapping("sale_chance")

public class SaleChanceController extends BaseController {
    @Resource
    private SaleChanceService saleChanceService;

    @RequiredPermission(code = "101001")
    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> querySaleChanceByParams(SaleChanceQuery saleChanceQuery,Integer flag,
                                                      HttpServletRequest request) {
        //判断flag的值
        if (flag != null && flag ==1) {
            //查询客户开发计划
            //设置分配状态
            saleChanceQuery.setState(StateStatus.STATED.getType());
            //设置指派人（当前登录用户的ID）
            //从cookie中获取当前用户的ID
            Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
            saleChanceQuery.setAssignMan(userId);
        }
        return saleChanceService.querySaleChanceByParams(saleChanceQuery);
    }
    @RequiredPermission(code = "1010")
    @RequestMapping("index")
    public String index() {
        return "saleChance/sale_chance";
    }

    @RequiredPermission(code = "101002")
    @RequestMapping("add")
    @ResponseBody
    public ResultInfo addSaleChance(SaleChance saleChance, HttpServletRequest request) {
        //从cookie中获取当前登录的用户名
        String userName = CookieUtil.getCookieValue(request,"userName");
        //设置用户名到营销机会对象
        saleChance.setCreateMan(userName);
        //调用Service层添加方法
        saleChanceService.addSaleChance(saleChance);
        return success("营销机会数据添加成功!!!");
    }

    /*更新营销机会*/
    @RequiredPermission(code = "101004")
    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateSaleChance(SaleChance saleChance) {
        //调用Service层添加方法
        saleChanceService.updateSaleChance(saleChance);
        return success("营销机会数据更新成功!!!");
    }

    /*进入添加/修改营销机会数据页面*/
    @RequestMapping("toSaleChancePage")
    public String toSaleChancePage(Integer saleChanceId,HttpServletRequest request) {
        //判断saleChanceId是否为空
        if (saleChanceId != null) {
            //通过ID查询营销机会数据
            SaleChance saleChance = saleChanceService.selectByPrimaryKey(saleChanceId);
            //将数据设置到请求域中
            request.setAttribute("saleChance",saleChance);
        }


        return "saleChance/add_update";
    }

    /*删除营销机会*/
    @RequiredPermission(code = "101003")
    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteSaleChance(Integer[] ids) {
        //调用Service层的删除方法
        saleChanceService.deleteSaleChance(ids);
        return success("营销机会数据删除成功！");
    }

    @PostMapping("updateSaleChanceDevResult")
    @ResponseBody
    public ResultInfo updateSaleChnceDevResult(Integer id,Integer devResult) {
        saleChanceService.updateSaleChnceDevResult(id,devResult);
        return success("开发状态更新!");
    }
}
